<script setup lang="ts" name="GoodsSku">
import { GoodsInfo, ValueType, Spec } from '@/types/goods'
import bwPowerset from '@/utils/bwPowerset'

const props = defineProps<{
  goodsInfo: GoodsInfo
  skuId?: string
}>()

const emit = defineEmits<{
  (e: 'checkedGoods', skuId: string): void
}>()

// 1. 控制类名
const changeSelected = (sub: ValueType, item: Spec) => {
  if (sub.disabled) return
  if (sub.selected) {
    sub.selected = false
  } else {
    item.values.forEach((value) => (value.selected = false))
    sub.selected = true
  }

  defaultDisabled()

  // 用户选择完整的规格之后提交数据 将skuId传递给后端
  const fullArr = orderKey().filter((v) => v)

  if (fullArr.length === props.goodsInfo.specs.length) {
    const submitSkuId = pathMap[fullArr.join('+')][0]
    emit('checkedGoods', submitSkuId)
  }
}

// 2. 获取路径字典
const getMap = () => {
  const obj: { [key: string]: string[] } = {}
  // 2.1 将有效的sku字段筛选处理
  const skus = props.goodsInfo.skus.filter((item) => item.inventory > 0)
  // console.log(skus)

  // 2.2 求子集 前提保证求子集的是一个数组
  skus.forEach((sku) => {
    // 2.2.1 将每一个sku的specs的每一项valueName收集到一个数组中
    const arr = sku.specs.map((spec) => spec.valueName)
    // console.log(arr)

    // 2.2.2 求子集
    const sets = bwPowerset(arr)
    // console.log(sets)

    // 2.3 将数据存入路径字典中
    sets.forEach((set) => {
      // 对象要存的键
      const key = set.join('+')

      if (obj[key]) {
        // 如果对象中存在key 累加
        obj[key].push(sku.id)
      } else {
        // 如果对象中不存在key 新增一个key属性
        obj[key] = [sku.id]
      }
    })
  })

  return obj
}
const pathMap = getMap()
// console.log(pathMap)

// 3. 基于路径字典做按钮的默认禁用 (单个)
const defaultDisabled = () => {
  // console.log(orderKey())
  const arr = orderKey()

  props.goodsInfo.specs.forEach((spec, index) => {
    spec.values.forEach((btnObj) => {
      const newArr = [...arr]
      newArr[index] = btnObj.name
      const key = newArr.filter((item) => item).join('+')
      if (pathMap.hasOwnProperty(key)) {
        btnObj.disabled = false
      } else {
        btnObj.disabled = true
      }
    })
  })
}
defaultDisabled()

// 4. 动态设置组合按钮禁用
function orderKey() {
  const arr: string[] = []
  props.goodsInfo.specs.forEach((spec, index) => {
    const btnObj = spec.values.find((btnObj) => btnObj.selected === true)

    if (btnObj) {
      arr[index] = btnObj.name
    } else {
      arr[index] = ''
    }
  })
  return arr
}

// 5. 默认规格选中
const defaultSelect = () => {
  if (!props.skuId) return

  const sku = props.goodsInfo.skus.find((item) => item.id === props.skuId)

  if (sku) {
    props.goodsInfo.specs.forEach((spec, index) => {
      spec.values.forEach((btnObj) => {
        if (btnObj.name === sku.specs[index].valueName) {
          btnObj.selected = true
        }
      })
    })
  }
}
defaultSelect()

// 6. 默认组合禁用
if (props.skuId) {
  defaultDisabled()
}
</script>
<template>
  <div class="goods-sku">
    <dl v-for="item in goodsInfo.specs" :key="item.id">
      <dt>{{ item.name }}</dt>
      <dd>
        <template v-for="sub in item.values">
          <img
            :src="sub.picture"
            alt=""
            v-if="sub.picture"
            :class="{ selected: sub.selected, disabled: sub.disabled }"
            @click="changeSelected(sub, item)"
          />
          <span
            v-else
            :class="{ selected: sub.selected, disabled: sub.disabled }"
            @click="changeSelected(sub, item)"
            >{{ sub.name }}</span
          >
        </template>
      </dd>
    </dl>
  </div>
</template>

<style scoped lang="less">
.sku-state-mixin () {
  border: 1px solid #e4e4e4;
  margin-right: 10px;
  cursor: pointer;
  // selected ==> 选中高亮
  &.selected {
    border-color: @xtxColor;
  }
  // disabled ==> 禁用 缺货
  &.disabled {
    opacity: 0.6;
    border-style: dashed;
    cursor: not-allowed;
  }
}
.goods-sku {
  padding-left: 10px;
  padding-top: 20px;
  dl {
    display: flex;
    padding-bottom: 20px;
    align-items: center;
    dt {
      width: 50px;
      color: #999;
    }
    dd {
      flex: 1;
      color: #666;
      > img {
        width: 50px;
        height: 50px;
        margin-top: 5px;
        .sku-state-mixin ();
      }
      > span {
        display: inline-block;
        height: 30px;
        line-height: 30px;
        padding: 0 20px;
        margin-top: 5px;
        .sku-state-mixin ();
      }
    }
  }
}
</style>
